package com.wowangz.cms.systemmanage.sms.smsset.service;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;

import com.google.gson.Gson;
import com.wowangz.cms.common.utils.FreeMarkerResolve;
import com.wowangz.cms.common.utils.SysConstant;
import com.wowangz.cms.syscore.api.UserContext;
import com.wowangz.cms.syscore.exception.ServiceException;
import com.wowangz.cms.syscore.service.BaseService;
import com.wowangz.cms.systemmanage.sms.send.entity.SendMessage;
import com.wowangz.cms.systemmanage.sms.smsset.entity.SmsSet;

import freemarker.template.TemplateException;

public class SmsSetService extends BaseService {

	public void smsSetInsert(SmsSet smsSet) throws ServiceException {
		try {
			this.getSqlSession().insert("SmsSetService.smsSetInsert", smsSet);
		} catch (ServiceException e) {
			throw new ServiceException(e.getMessage());
		}
	}

	/**
	 * @描述： 修改
	 * 
	 * @author HUANGYUAN
	 * @TIME 2015年10月9日 下午2:53:58
	 * @param smsSet
	 * @throws ServiceException
	 */
	public void smsSetUpdate(SmsSet smsSet) throws ServiceException {
		try {
			this.getSqlSession().update("SmsSetService.smsSetUpdate", smsSet);
		} catch (ServiceException e) {
			throw new ServiceException(e.getMessage());
		}
	}

	/**
	 * @描述：逻辑删除
	 * @author HUANGYUAN
	 * @TIME 2015年10月9日 下午2:52 55
	 * @param smsSet
	 * @throws ServiceException
	 */
	public void smsSetDelete(SmsSet smsSet) throws ServiceException {
		try {
			this.getSqlSession().update("SmsSetService.smsSetUpdate", smsSet);
		} catch (ServiceException e) {
			throw new ServiceException(e.getMessage());
		}
	}

	/**
	 * @描述：查询一条记录
	 * @author HUANGYUAN
	 * @TIME 2015年10月9日 下午2:55:02
	 * @param paramMap
	 * @return SmsSet
	 * @throws ServiceException
	 */
	public SmsSet querySmsSetById(Map<String, Object> paramMap) throws ServiceException {
		try {
			SmsSet smsSet = this.getSqlSession().selectOne("SmsSetService.querySmsSetById", paramMap);
			return smsSet;
		} catch (ServiceException e) {
			throw new ServiceException(e.getMessage());
		}
	}

	/**
	 * @描述：查询结果集的数量
	 * @author HUANGYUAN
	 * @TIME 2015年10月9日 下午2:55:51
	 * @param paramMap
	 * @return long
	 * @throws ServiceException
	 */
	public long querySmsSetCount(Map<String, Object> paramMap) throws ServiceException {
		try {
			return this.getSqlSession().selectOne("SmsSetService.querySmsSetCount", paramMap);
		} catch (ServiceException e) {
			throw new ServiceException(e.getMessage());
		}
	}

	/**
	 * @描述：查询结果集
	 * @author HUANGYUAN
	 * @TIME 2015年10月9日 下午2:57:08
	 * @param paramMap
	 * @return List<SmsSet>
	 * @throws ServiceException
	 */
	public List<SmsSet> querySmsSetPage(Map<String, Object> paramMap) throws ServiceException {
		try {
			return this.getSqlSession().selectList("SmsSetService.querySmsSetPage", paramMap);
		} catch (ServiceException e) {
			throw new ServiceException(e.getMessage());
		}
	}

	public Map<String, String> getMap(SmsSet smsSet, UserContext userContext) {
		Set<String> parameter = SysConstant.Templatetype.getSmsSetParameter(smsSet.getTemplatetype().toString());
		Map<String, String> variable = new HashMap<String, String>();
		for (Iterator<String> iterator = parameter.iterator(); iterator.hasNext();) {
			String key = (String) iterator.next();
			if (StringUtils.equals(key, "password")) {
				variable.put(key, userContext.getLoginUser().getPassword());
			} else if (StringUtils.equals(key, "amount")) {
				variable.put(key, "10");
			} else if (StringUtils.equals(key, "username")) {
				variable.put(key, userContext.getLoginUser().getUsername());
			} else if (StringUtils.equals(key, "curenttime")) {
				variable.put(key, new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date()));
			}
		}
		return variable;
	}

	// 加入堆栈中
	public void sendMessage(SendMessage message, boolean isTemplate, SmsSet smsSet, Map<String, String> paMap) throws IOException, TemplateException {
		if (isTemplate) {
			String content = FreeMarkerResolve.freemarkerProcess(paMap, smsSet.getTemplatecontent());
			message.setMessage(content);
		}
		this.sendTransDataToMq(new Gson().toJson(message), SysConstant.MQQUEUE.MQ_SEND_SMS);
	}
	
	/**
	 * 查询短信模板
	 * @param paramsMap
	 * @return
	 * @throws ServiceException
	 */
	public SmsSet querySmsSetByMap(Map<String, Object> paramsMap) throws ServiceException {
		return this.getSqlSession().selectOne("SmsSetService.querySmsSetByMap", paramsMap);
	}
}
